package com.numberone.web.controller.xycard;

import com.numberone.framework.web.base.BaseController;
import com.numberone.system.domain.XyCard;
import com.numberone.system.domain.XyUser;
import com.numberone.system.service.IXyCardService;
import com.numberone.system.service.IXyUserService;
import com.numberone.system.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/api/XyCard")
public class XyCardApiController extends BaseController {

    @Autowired
    private IXyCardService xyCardService;
    @Autowired
    private IXyUserService xyUserService;

    /**
     * 使用欣亿卡
     * @param xyCard
     * @return
     */
    @ResponseBody
    @PostMapping("/useCard")
    public Object userCard(Long userId, XyCard xyCard)
    {
        int isCardNum = xyCardService.isCardNum(xyCard.getCardNum());
        if (isCardNum <= 0)
        {
            return JsonResult.noCard();
        }
        XyCard card = xyCardService.selectXyCardByNum(xyCard.getCardNum());
        if (card.getStatus() != 0)
        {
            return JsonResult.noCard();
        }
        if (!xyCard.getCardPwd().equals(card.getCardPwd()))
        {
            return JsonResult.pwdError();
        }
        XyUser xyUser = xyUserService.selectXyUserById(userId);
        System.out.println(card.getPrice()+"=====");
        xyUser.setOverage(xyUser.getOverage().add(card.getPrice()));
        xyUserService.updateXyUser(xyUser);
        card.setStatus(1);
        int i = xyCardService.updateXyCard(card);
        if (i > 0)
        {
            return JsonResult.success();
        }
        return JsonResult.noCard();
    }

    /**
     * 查询账户余额
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("/selectOverage")
    public Object selectOverage(Long userId)
    {
        XyUser xyUser = xyUserService.selectXyUserById(userId);
        return JsonResult.success(xyUser.getOverage());
    }
}
